package com.czmini.cztest.letcode.array;

import org.testng.annotations.Test;

import java.util.Arrays;

/**
 * @ProjectName: cztest
 * @Package: com.czmini.cztest.letcode.array
 * @ClassName: ccopyDEMO1
 * @Author: 还是那个橙子
 * @Description:
 * @Date: 2022/4/22 16:28
 * @Version: 1.0
 */

public class ccopyDEMO1 {

        @Test
        public  void test1(){
        String[] originArr = new String[]{"唐僧","孙悟空","猪八戒","沙僧"};
        // 1.当输入的newLength小于0（不合法）时，抛出java.lang.NegativeArraySizeException异常
        // String[] newArr1 = Arrays.copyOf(originArr, -2);
        // printArr(newArr1);
        // 2.当输入的newLength小于源数组长度时
        String[] newArr2 = Arrays.copyOf(originArr, 3);
        printArr(newArr2);
        // 3.当输入的newLength大于源数组长度时，新数组没有填充的元素是默认值
        String[] newArr3 = Arrays.copyOf(originArr, 6);
        printArr(newArr3);
    }
 @Test
    public void test2(){
        String[] originArr = new String[]{"唐僧", "孙悟空", "猪八戒", "沙僧"};
        String[] newOriginArr =new String[]{"唐搜僧", "孙2悟空", "猪22八戒", "沙33僧"};
        String[] newArr = Arrays.copyOfRange(originArr, 0, 2); // 取第1个元素到第三个元素

     //  System.arraycopy(newOriginArr, 0, originArr,0, 4);

        printArr(newArr);



//     a%b=a-(a/b)*b

    }
    @Test
    public void test3(){
        // 源数组
        String[] originArr1 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        // 目标数组
        String[] descArr = new String[4];
        descArr[0] = "小白龙";
        //从下标为0的位置开始复制，复制的长度为4(复制 1234 )，从下标为3的位置开始替换为1234
        System.arraycopy(originArr1, 0, originArr1, 3, 4);
        //结果  1	2	3	1	2	3	4	8	9	10
        // 调用方法，复制源数组从索引为1的位置开始的2个元素，然后复制到目标数组从索引1开始的两个元素
       // System.arraycopy(originArr,  1, descArr, 1, 2);
        // 打印目标数组
        printArr(originArr1);

    }

    @Test
    public  void test4(){
        String[] arr = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        String[] copyOfRange = Arrays.copyOfRange(arr, 0, 3);
        System.arraycopy(arr, 0, arr, 3, 4);

        printArr(arr);;


    }

    static void printArr(String[] arr) {
        for (String s : arr) {
            System.out.print(s + "\t");
        }
        System.out.println();
    }
}
